package com.wuzuqing.android.eightdiagrams.utils

import org.json.JSONObject
import java.io.OutputStreamWriter
import java.net.HttpURLConnection
import java.net.URL
import java.util.*

/**
 * @CopyRight: 广州仰望星空云科技有限公司
 * @Author : wuzuqing
 * @Email:wuzuqing@xinyu668.com
 * @CreateTime: 15.8.24 10:14 AM
 * @Description: 请写描述
 */
object HttpUtils {

    fun buildP3Url(pageNo: Int, pageSize: Int = 100): String {
        return "https://webapi.sporttery.cn/gateway/lottery/getHistoryPageListV1.qry" +
                "?gameNo=35&provinceId=0&pageSize=${pageSize}&isVerify=1&pageNo=${pageNo}"
    }

    fun build3DUrl(pageNo: Int, pageSize: Int = 100): String {
        return "https://www.cwl.gov.cn/cwl_admin/front/cwlkj/search/kjxx/findDrawNotice" +
                "?name=3d&systemType=PC&pageSize=${pageSize}&pageNo=${pageNo}"
    }

    fun getP3Result(pageNo: Int, pageSize: Int = 100): JSONObject? {
        val url = URL(buildP3Url(pageNo, pageSize))
        val httpConn = url.openConnection() as HttpURLConnection
        httpConn.requestMethod = "GET"
        val responseStream =
            if (httpConn.responseCode / 100 == 2) httpConn.inputStream else httpConn.errorStream
        val s = Scanner(responseStream).useDelimiter("\\A")
        val result = if (s.hasNext()) s.next() else ""
        if (result.isEmpty()) {
            return null
        }
        try {
            val jsonObject = JSONObject(result)
            return jsonObject.getJSONObject("value")
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return null
    }

    fun get3DResult(pageNo: Int, pageSize: Int = 100): JSONObject? {
        val url = URL(build3DUrl(pageNo, pageSize))
        val httpConn = url.openConnection() as HttpURLConnection
        httpConn.requestMethod = "GET"
        httpConn.setRequestProperty(
            "Accept",
            "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7"
        )
        httpConn.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.9")
        httpConn.setRequestProperty("Connection", "keep-alive")
        httpConn.setRequestProperty("Sec-Fetch-Dest", "document")
        httpConn.setRequestProperty("Sec-Fetch-Mode", "navigate")
        httpConn.setRequestProperty("Sec-Fetch-Site", "none")
        httpConn.setRequestProperty("Sec-Fetch-User", "?1")
        httpConn.setRequestProperty("Upgrade-Insecure-Requests", "1")
        httpConn.setRequestProperty(
            "sec-ch-ua",
            "\"Not/A)Brand\";v=\"8\", \"Chromium\";v=\"126\", \"Google Chrome\";v=\"126\""
        )
        httpConn.setRequestProperty("sec-ch-ua-mobile", "?0")
        httpConn.setRequestProperty("sec-ch-ua-platform", "macOS")
        httpConn.setRequestProperty(
            "Cookie",
            "21_vq=5;HMF_CI=4956d575ea36ee688a78e8aa60715c5ecd85975f007c03cbb76c1e48dc9c5030919c1b5c0433d0d29d9001fd6b51164e88e480270261905c5310cd409751c51fd2"
        )
        httpConn.setRequestProperty(
            "Referer",
            "https://www.cwl.gov.cn/cwl_admin/front/cwlkj/search/kjxx/findDrawNotice?name=3d&issueCount=&issueStart=&issueEnd=&dayStart=&dayEnd=&pageNo=1&pageSize=100&week=&systemType=PC"
        )

        httpConn.setRequestProperty(
            "User-Agent",
            "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36"
        )

//        httpConn.doOutput = true
        val code = httpConn.responseCode
        val responseStream =
            if (httpConn.responseCode / 100 == 2) httpConn.inputStream else httpConn.errorStream
        LogUtils.d("responseCode:$code")
        val s = Scanner(responseStream).useDelimiter("\\A")
        val result = if (s.hasNext()) s.next() else ""
        if (result.isEmpty()) {
            return null
        }
        try {
            val jsonObject = JSONObject(result)
            return jsonObject.getJSONObject("result")
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return null
    }

}